'use strict';
module.exports = function (sequelize, DataTypes) {

  var Organization = sequelize.define('Organization', {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      primaryKey: true,
      autoIncrement: true
    },
    parentId: {
      type: DataTypes.INTEGER.UNSIGNED,
      comment: '父ID'
    },
    roleId: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
      validate: {
        isInt: true
      },
      comment: '角色Id'
    },
    storeId: {
      type: DataTypes.STRING(41),
      comment: '店铺ID'
    },
    storeName: {
      type: DataTypes.STRING(41),
      comment: '店铺名称'
    },
    partyGroup: {
      type: DataTypes.STRING(41),
      comment: '店铺名称'
    },
    name: {           // 名称，storeName或者partyGroup
      type: DataTypes.STRING(41),
      allowNull: false,
      comment: '分组名称'
    }
  }, {
    classMethods: {
      associate: function (models) {
        Organization.hasMany(Organization, {
          as: 'children',
          foreignKey: 'parentId',
          through: null
        });

        Organization.belongsTo(models.Role, {
          foreignKey: 'roleId',
          constraints: false
        });
      }
    },
    timstamps: true,
    paranoid: true,
    indexes: [
      {
        unique: true,
        fields: ['name']
      }
    ],
    tableName: 'organizations'
  });

  return Organization;
};
